//
//  SVideoListViewController.m
//  Tiger
//
//  Created by  on 12-8-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SVideoListViewController.h"
#import "asyncimageview.h"
#import <MediaPlayer/MediaPlayer.h>
#import "TigerAppDelegate.h"
@interface SVideoListViewController ()

@end

@implementation SVideoListViewController

-(id) init
{
    if (self = [super init]) {
        
    }
    return self;
}
-(void) dealloc
{
    [p_URLRequest setDelegate:nil];
    [p_URLRequest release];
    [p_ScrollContent release];
    [p_DicArrData release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    UIImageView*  tBGImgView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"screen_bg.png"]];
    tBGImgView.frame =CGRectMake(0, 0, 320, 480-88);
    [self.view addSubview:tBGImgView];
    [tBGImgView release];
    
    p_ScrollContent = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-88)];
    [self.view addSubview:p_ScrollContent];
    
    NSString* tURLString = [NSString stringWithFormat:@"http://www.speedtiger.com.cn/ios3/video_list.html"];
    p_URLRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:tURLString]];
    [p_URLRequest setDelegate:self];
    [p_URLRequest startAsynchronous];
    [p_URLRequest retain];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

-(void) videoPressed:(UIButton*)f_sender
{
    int tType = f_sender.tag-300;
    NSDictionary* tDicData =[p_DicArrData objectAtIndex:tType];
    NSString* tURLStr =[tDicData objectForKey:@"videoUrl"];
    MPMoviePlayerViewController* tMMPC = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:tURLStr]];
    TigerAppDelegate* tAppDel = (TigerAppDelegate*)[UIApplication sharedApplication].delegate;
    
    NSLog(@"%@",[tAppDel.customviewController description]);
    [tAppDel.customviewController presentMoviePlayerViewControllerAnimated:tMMPC];
    [tMMPC release];
}

#pragma mark-
#pragma mark ASIHTTPRequestDelegate
- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"%@",[request responseString]);
    NSData *responseData = [request responseData];
	NSString *responseStr = [[[NSString alloc] initWithData:responseData encoding: NSUTF8StringEncoding] autorelease];
    

    NSDictionary* tEnumDicData = [responseStr JSONValue];

    p_DicArrData = [tEnumDicData objectForKey:@"items"];
    [p_DicArrData retain];
    int tDataIndex = 0;
    for (NSDictionary* tDic in p_DicArrData) {
        NSString* tOneImg = [tDic objectForKey:@"picUrl"];
        UIButton* tPressBtt = [UIButton buttonWithType:UIButtonTypeCustom];
        [tPressBtt addTarget:self action:@selector(videoPressed:) forControlEvents:UIControlEventTouchUpInside];
        tPressBtt.tag = 300+tDataIndex;
        int tPostx = 6+(tDataIndex%4)*78;
        int tPosty = 6+(tDataIndex/4)*78;
        tPressBtt.frame = CGRectMake(tPostx, tPosty, 74, 74);
        [p_ScrollContent addSubview:tPressBtt];
        if ([tOneImg length] > 0) {
            AsyncImageView* tAsImg =[[AsyncImageView alloc] initWithFrame:CGRectMake(0, 0, 74, 74)];
            tAsImg.userInteractionEnabled =NO;
            tAsImg._numStateRequested = need_Request;
            tAsImg._imageRequestURL = tOneImg;
            tAsImg.m_BoolSetImage = YES;
            [tPressBtt addSubview:tAsImg];
            [tAsImg customLoadImageFromURL];
        }
        tDataIndex++;
    }
    
    [p_ScrollContent setContentSize:CGSizeMake(320, [p_DicArrData count]/4*80)];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
	UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"无可用的网络连接" delegate:self 
										   cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
	[alert addButtonWithTitle:@"确定"];
	[alert show];
}

@end
